package distributed_core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Map;

import com.rabbitmq.client.QueueingConsumer;

public class InvocationHandler {
	
	public Object invokeRemote(String className, String methodName, Map<Class<?>, Object> args, Map<String, String> properties, String queueName) throws Exception {
		
//		RemoteInvocationArguments arguments = new RemoteInvocationArguments(className, methodName, args, properties, Activator.exchangeName);
//		
//		ObjectOutput out;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		out = new ObjectOutputStream(bos);
		out.writeObject(arguments);
		out.close();
		
		Activator.senderChannel.basicPublish("", "SENDER_QUEUE", null, bos.toByteArray());
		
		Activator.receiverChannel.basicConsume("RECEIVER_QUEUE", true, Activator.receiverConsumer);
		QueueingConsumer.Delivery delivery = Activator.receiverConsumer.nextDelivery();
////		
		ObjectInput in;
		ByteArrayInputStream bais = new ByteArrayInputStream(delivery.getBody());
		in = new ObjectInputStream(bais);
		Object res = in.readObject();
		return res;
		return null;
	}

}
